﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data;
using System.Data.Common;
using System.Data.SqlServerCe;
using System.Reflection;

namespace SmartDeviceProject1
{
    public partial class Form1 : Form
    {
        //public string GetCurrentDirectory()
        //{
        //    return Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
        //}
        //string connectionString = System.IO.Path.Combine(GetCurrentDirectory(), "ls.sdf");
        //private string GetExePath()
        //{
        //    return (Path.GetDirectoryName(Assembly.GetExecutingAssembly.GetName.CodeBase));
        //}
        //public void CreateNewDatabase()
        //{
        //    if(File.Exists("tempdb.sdf")) File.Delete("tempdb.sdf");
        //    string connStr = "Data Source = tempdb.sdf;Password = testing123";
        //    using(SqlCeEngine engine = new SqlCeEngine(connStr))
        //    {
        //        engine.CreateDatabase();
        //    }
        //}
       
       

        #region - Biến Toàn Cục -

       public string connectionString = @"Data Source =\Projects\SmartDeviceProject1\SmartDeviceProject1\App-code\db.sdf";
       // private bool isInsert = true;

        #endregion
        #region - Load Du Lieu -
        private DataTable LoadData()
        {
            DataTable dt = null;
            SqlCeConnection conn = new SqlCeConnection(connectionString);
            try
            {
                conn.Open();
                string select = "Select * From NHANVIEN";
                SqlCeCommand cmd = new SqlCeCommand(select, conn);
                SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
                dt = new DataTable();
                da.Fill(dt);
            }
            catch (Exception ex)
            {
                //Tạm thời chưa xử lý lỗi 
            }
            finally
            {
                conn.Close();
            }
            return dt;
        }
        #endregion
        #region - Cập Nhật Dữ Liệu cho combobox cbMaCN -
        private bool cbATM()
        {
            bool success = false;
            SqlCeConnection conn = new SqlCeConnection(connectionString);
            conn.Open();
            string search = "Select id  from ATM";
            SqlCeCommand cmd = new SqlCeCommand(search, conn);
            SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            comboBox1.DataSource = dt;
            comboBox1.DisplayMember = "name";            
            return success;
        }
        #endregion

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cbATM();
            dataGrid1.DataSource = LoadData();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}